{% load static %}
<!-- 资产图片管理组件 -->
<div class="card mb-4">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">资产图片管理 
            <small class="text-muted">({{ asset.get_image_count }}/10)</small>
        </h5>
        <div>
            <div class="btn-group" role="group">
                {% if asset.can_add_more_images %}
                    <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#uploadImageModal">
                        <i class="fa fa-plus"></i> 上传图片
                    </button>
                {% endif %}
                {% if asset.get_all_images %}
                    <button type="button" class="btn btn-sm btn-outline-secondary js-toggle-batch">
                        <i class="fa fa-check-square-o"></i> 批量管理
                    </button>
                {% endif %}
            </div>
            {% if not asset.can_add_more_images %}
                <div class="mt-1">
                    <span class="text-muted small">已达到图片上限</span>
                </div>
            {% endif %}
        </div>
    </div>
    <div class="card-body">
        <!-- 批量操作工具栏 -->
        <div id="batchToolbar" class="alert alert-info" style="display: none;">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <i class="fa fa-info-circle"></i>
                    已选择 <span id="selectedCount">0</span> 张图片
                </div>
                <div>
                    <button type="button" class="btn btn-sm btn-danger js-batch-delete" disabled id="batchDeleteBtn">
                        <i class="fa fa-trash"></i> 批量删除
                    </button>
                    <button type="button" class="btn btn-sm btn-secondary js-batch-cancel" id="batchCancelBtn">
                        <i class="fa fa-times"></i> 取消
                    </button>
                </div>
            </div>
        </div>
        
        {% if asset.get_all_images %}
            <div class="row">
                {% for image in asset.get_all_images %}
                    <div class="col-md-4 mb-3">
                        <div class="card image-card" data-image-id="{{ image.id }}">
                            <div class="position-relative">
                                <!-- 批量选择复选框 -->
                                <div class="position-absolute top-0 start-0 m-2 batch-checkbox" style="display: none;">
                                    <input type="checkbox" class="form-check-input image-checkbox js-image-checkbox" 
                                           value="{{ image.id }}">
                                </div>
                                
                                <img src="{{ image.url }}" class="card-img-top js-image-preview" alt="{{ image.title }}"
                                     style="height: 200px; object-fit: cover; cursor: pointer;"
                                     data-image-url="{{ image.url }}" data-image-title="{{ image.title|escapejs }}">
                                {% if image.is_primary %}
                                    <span class="position-absolute top-0 start-0 badge bg-primary m-2 primary-badge">主图片</span>
                                {% endif %}
                                <div class="position-absolute top-0 end-0 m-2 normal-actions">
                                    <div class="dropdown">
                                        <button class="btn btn-sm btn-light dropdown-toggle" type="button" 
                                                data-bs-toggle="dropdown" aria-expanded="false">
                                            <i class="fa fa-ellipsis-v"></i>
                                        </button>
                                        <ul class="dropdown-menu">
                                            <li>
                                                <a class="dropdown-item js-edit-image" href="#" 
                                                   data-image-id="{{ image.id }}" data-image-title="{{ image.title|escapejs }}" data-image-desc="{{ image.description|escapejs }}">
                                                    <i class="fa fa-edit"></i> 编辑信息
                                                </a>
                                            </li>
                                            {% if not image.is_primary and image.type != 'main' %}
                                                <li>
                                                    <a class="dropdown-item js-set-primary" href="#" 
                                                       data-image-id="{{ image.id }}">
                                                        <i class="fa fa-star"></i> 设为主图片
                                                    </a>
                                                </li>
                                            {% endif %}
                                            <li><hr class="dropdown-divider"></li>
                                            <li>
                                                <a class="dropdown-item text-danger js-delete-image" href="#" 
                                                   data-image-id="{{ image.id }}" data-image-title="{{ image.title|escapejs }}">
                                                    <i class="fa fa-trash"></i> 删除图片
                                                </a>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <div class="card-body p-2">
                                <h6 class="card-title mb-1">{{ image.title|default:"未命名" }}</h6>
                                {% if image.description %}
                                    <p class="card-text small text-muted">{{ image.description|truncatechars:50 }}</p>
                                {% endif %}
                                <div class="d-flex justify-content-between align-items-center">
                                    <small class="text-muted">
                                        {% if image.uploaded_by %}{{ image.uploaded_by }}{% endif %}
                                        {% if image.created_at %}{{ image.created_at|date:"m-d" }}{% endif %}
                                    </small>
                                    {% if image.file_size %}
                                        <small class="text-muted">{{ image.file_size }}MB</small>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        {% else %}
            <div class="text-center py-4">
                <i class="fa fa-image fa-3x text-muted mb-3"></i>
                <p class="text-muted">暂无图片</p>
                <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#uploadImageModal">
                    <i class="fa fa-plus"></i> 上传第一张图片
                </button>
            </div>
        {% endif %}
    </div>
</div>

<!-- 资产文件管理组件 -->
<div class="card mb-4">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">资产文件管理 
            <small class="text-muted">({{ asset.get_file_count }}/20)</small>
        </h5>
        <div>
            {% if asset.can_add_more_files %}
                <button type="button" class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#uploadFileModal">
                    <i class="fa fa-upload"></i> 上传文件
                </button>
            {% else %}
                <span class="text-muted small">已达到文件上限</span>
            {% endif %}
        </div>
    </div>
    <div class="card-body">
        {% if asset.get_all_files %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th width="50">类型</th>
                            <th>文件名</th>
                            <th>描述</th>
                            <th width="80">大小</th>
                            <th width="100">上传人</th>
                            <th width="80">日期</th>
                            <th width="100">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for file in asset.get_all_files %}
                            <tr>
                                <td class="text-center">
                                    <i class="fa {{ file.file_icon }} fa-lg text-primary"></i>
                                </td>
                                <td>
                                    <a href="{% url 'assets:asset_file_view' asset.id file.id %}" target="_blank" class="text-decoration-none">
                                        <strong>{{ file.title }}</strong>
                                    </a>
                                </td>
                                <td>
                                    <small class="text-muted">{{ file.description|truncatechars:50|default:"-" }}</small>
                                </td>
                                <td>
                                    <small class="text-muted">{{ file.file_size }}</small>
                                </td>
                                <td>
                                    <small class="text-muted">{{ file.uploaded_by|default:"-" }}</small>
                                </td>
                                <td>
                                    <small class="text-muted">{{ file.created_at|date:"m-d" }}</small>
                                </td>
                                <td>
                                    <div class="btn-group btn-group-sm">
                                        <button type="button" class="btn btn-outline-secondary btn-sm js-view-file"
                                                title="查看"
                                                data-view-url="{% url 'assets:asset_file_view' asset.id file.id %}"
                                                data-file-type="{{ file.file_type|default:'' }}"
                                                data-file-title="{{ file.title|escapejs }}">
                                            <i class="fa fa-eye"></i>
                                        </button>
                                        <button type="button" class="btn btn-outline-primary btn-sm js-edit-file"
                                                title="编辑"
                                                data-file-id="{{ file.id }}"
                                                data-file-title="{{ file.title|escapejs }}"
                                                data-file-desc="{{ file.description|escapejs }}">
                                            <i class="fa fa-edit"></i>
                                        </button>
                                        <button type="button" class="btn btn-outline-danger btn-sm js-delete-file"
                                                title="删除"
                                                data-file-id="{{ file.id }}"
                                                data-file-title="{{ file.title|escapejs }}">
                                            <i class="fa fa-trash"></i>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        {% else %}
            <div class="text-center py-4">
                <i class="fa fa-file fa-3x text-muted mb-3"></i>
                <p class="text-muted">暂无文件</p>
                <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#uploadFileModal">
                    <i class="fa fa-upload"></i> 上传第一个文件
                </button>
            </div>
        {% endif %}
    </div>
</div>

<!-- 在页面中注入 asset_id 与当前图片数量，便于外部 JS 使用 -->
<input type="hidden" name="asset_id" value="{{ asset.id }}">
<input type="hidden" name="asset_image_count" value="{{ asset.get_image_count }}">

<!-- 上传图片模态框 -->
<div class="modal fade" id="uploadImageModal" tabindex="-1" aria-labelledby="uploadImageModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="uploadImageModalLabel">上传资产图片</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="post" action="/asset/{{ asset.id }}/upload-images/" enctype="multipart/form-data">
                {% csrf_token %}
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="uploadImages" class="form-label">选择图片 *</label>
                        <input type="file" class="form-control" id="uploadImages" name="images" 
                               accept="image/*" multiple required>
                        <div class="form-text">支持 JPG、PNG、GIF 格式，单张图片不超过 2MB，可同时选择多张图片</div>
                    </div>
                    
                    <!-- 图片预览区域 -->
                    <div id="imagesPreview" class="mb-3" style="display: none;">
                        <label class="form-label">图片预览</label>
                        <div id="previewContainer" class="row"></div>
                    </div>
                    
                    <!-- 批量设置区域 -->
                    <div id="batchSettings" class="mb-3" style="display: none;">
                        <h6>批量设置</h6>
                        <div class="row">
                            <div class="col-md-6">
                                <label for="batchTitle" class="form-label">标题前缀</label>
                                <input type="text" class="form-control" id="batchTitle" name="batch_title" 
                                       placeholder="为所有图片添加标题前缀">
                            </div>
                            <div class="col-md-6">
                                <label for="batchDescription" class="form-label">统一描述</label>
                                <input type="text" class="form-control" id="batchDescription" name="batch_description" 
                                       placeholder="为所有图片添加统一描述">
                            </div>
                        </div>
                        <div class="mt-2">
                            <div class="form-check">
                                <input type="checkbox" class="form-check-input" id="setFirstAsPrimary" name="set_first_as_primary">
                                <label class="form-check-label" for="setFirstAsPrimary">
                                    将第一张图片设为主图片
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary" id="uploadImagesBtn">上传图片</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑图片信息模态框 -->
<div class="modal fade" id="editImageModal" tabindex="-1" aria-labelledby="editImageModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editImageModalLabel">编辑图片信息</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="editImageForm" method="post" action="/asset/{{ asset.id }}/edit-image/">
                {% csrf_token %}
                <input type="hidden" id="editImageId" name="image_id">
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="editImageTitle" class="form-label">图片标题</label>
                        <input type="text" class="form-control" id="editImageTitle" name="title">
                    </div>
                    <div class="mb-3">
                        <label for="editImageDescription" class="form-label">图片描述</label>
                        <textarea class="form-control" id="editImageDescription" name="description" rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存更改</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 上传文件模态框 -->
<div class="modal fade" id="uploadFileModal" tabindex="-1" aria-labelledby="uploadFileModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="uploadFileModalLabel">上传资产文件</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="post" action="/asset/{{ asset.id }}/upload-file/" enctype="multipart/form-data">
                {% csrf_token %}
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="uploadFile" class="form-label">选择文件 *</label>
                        <input type="file" class="form-control" id="uploadFile" name="file" 
                               accept=".pdf,.doc,.docx,.xls,.xlsx,.txt" required>
                        <div class="form-text">支持 PDF、Word、Excel、文本文件，大小不超过 10MB</div>
                    </div>
                    <div class="mb-3">
                        <label for="fileTitle" class="form-label">文件标题</label>
                        <input type="text" class="form-control" id="fileTitle" name="title" placeholder="为文件添加标题">
                    </div>
                    <div class="mb-3">
                        <label for="fileDescription" class="form-label">文件描述</label>
                        <textarea class="form-control" id="fileDescription" name="description" rows="3" 
                                  placeholder="描述文件内容或用途"></textarea>
                    </div>
                    <!-- 文件信息预览区域 -->
                    <div id="filePreview" class="mb-3" style="display: none;">
                        <label class="form-label">文件信息</label>
                        <div class="alert alert-info">
                            <div id="fileInfo"></div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-success">上传文件</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑文件信息模态框 -->
<div class="modal fade" id="editFileModal" tabindex="-1" aria-labelledby="editFileModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editFileModalLabel">编辑文件信息</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="editFileForm" method="post" action="/asset/{{ asset.id }}/edit-file/">
                {% csrf_token %}
                <input type="hidden" id="editFileId" name="file_id">
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="editFileTitle" class="form-label">文件标题</label>
                        <input type="text" class="form-control" id="editFileTitle" name="title">
                    </div>
                    <div class="mb-3">
                        <label for="editFileDescription" class="form-label">文件描述</label>
                        <textarea class="form-control" id="editFileDescription" name="description" rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存更改</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 移除内联脚本，改由外部静态 JS 管理 -->

<!-- 文件预览模态框（用于 PDF 内联预览） -->
<div class="modal fade" id="filePreviewModal" tabindex="-1" aria-labelledby="filePreviewTitle" aria-hidden="true">
    <div class="modal-dialog modal-xl modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="filePreviewTitle">文件预览</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body p-0" style="height: 80vh;">
                <iframe id="filePreviewFrame" src="" style="border:0; width:100%; height:100%;"></iframe>
            </div>
        </div>
    </div>
    </div>